<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <title></title>
    <script src="js/mui.min.js"></script>
    <link href="css/mui.min.css" rel="stylesheet"/>
    <link rel="stylesheet" href="css/iconfont.css" />
    <link rel="stylesheet" href="css/style.css" />	
    <script src="js/jquery-3.1.1.min.js"></script>
    <script type="text/javascript" src="js/publicurl.js" ></script>
</head>
<body>
	<header class="mui-bar mui-bar-nav">
	    <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
	    <h1 class="mui-title">完善信息</h1>
	</header>
	<div class="mui-content">
	    <div class="add-address-div" >
	    	<form class="mui-input-group public-lable-txt" >
	    	    <div class="mui-input-row" style="margin-top: 15px;">
	    	        <label>我的账号：</label>
	    	        <input type="text"readonly="readonly" id="username" name="username"  value="" >
	    	    </div>
	    	    <div class="mui-input-row">
	    	        <label>姓名：</label>
	    	        <input type="text"name="name" id="name"  class="mui-input-clear" placeholder="请输入真实姓名">
	    		</div>
	    	    <div class="mui-input-row mui-radio mui-right">
					<label>先生</label>
					<input name="userSex" id="userSex1" value="1" type="radio">
				</div>
				<div class="mui-input-row mui-radio mui-right">
					<label>女士</label>
					<input name="userSex" id="userSex2" value="0" type="radio" >
				</div>
	    	    <div class="mui-input-row">
	    	        <label>支付宝账号：</label>
	    	        <input type="text" id="payphone" class="mui-input-clear" placeholder="请输入支付宝账号">
	    		</div>
	    		<div class="mui-input-row">
	    	        <label>身份证号：</label>
	    	        <input type="text" id="userCard" class="mui-input-clear" placeholder="请输入身份证号">
	    		</div>
	    		<div class="mui-input-row">
	    	        <label>所在地：</label>
	    	        <input type="text" id="userAddr" class="mui-input-clear" placeholder="省/市/区">
	    		</div>
	    		
	    	</form>
	    	<button onclick="updateinfo()" class="public-g-btn" >确定</button>
	    </div>
	</div>
	<script src="js/castapp.js"></script>
	<script>
		mui.init();
		
		var userId = JSON.parse(localStorage.getItem("userInfo"));
		var url = localStorage.getItem("url");

		mui.ajax(url+"/user/getUser", {
			data: {
					id: userId,
				},

					//服务器返回json格式数据 
					type: 'post', //HTTP请求类型
					timeout: 10000, //超时时间设置为10秒；
					success: function(data) {
						if(data.code==0){
							$("#name").val(data.data.name);
		                    $("#username").val(data.data.username);
		                    
		                     if(data.data.userSex == 0){
						        $("#userSex1").attr("checked",false);
						        $("#userSex2").attr("checked",true);
						      }
						      if(data.data.userSex == 1){
						        $("#userSex1").attr("checked",true);
						        $("#userSex2").attr("checked",false);
						      }
		                    //$("input[name='userSex']:eq('"+data.data.userSex+"')").attr("checked",true);

							$("#payphone").val(data.data.payphone);
							$("#userCard").val(data.data.userCard);
							$("#userAddr").val(data.data.userAddr);
						}

						
					},
					error: function(xhr, type, errorThrown) { //异常处理；
						console.log(type);
					}
			});

		/*mui.ajax(puburl.url+'/dangxia/user/getUserPartialInfo', {
			data: {
				username:$("#username").val(),
			},
			dataType: 'json', //服务器返回json格式数据
			type: 'post', //HTTP请求类型
			timeout: 10000, //超时时间设置为10秒；
			success: function(partialInfo) {
				if(partialInfo.code == 0) {
					console.log(partialInfo.data.name);
				$("#name").val(partialInfo.data.name);
				$("#userSex").val(partialInfo.data.userSex);
				$("#payphone").val(partialInfo.data.payphone);
				$("#userCard").val(partialInfo.data.userCard);
				$("#userAddr").val(partialInfo.data.userAddr);

				}else{
					mui.toast("查询出错", {
						duration: 'short',
						type: 'div'
					});
					return;
				}
			},
			error: function(xhr, type, errorThrown) { //异常处理；
				console.log(type);
			}
		});*/
		
		/* 先发送ajax请求得到已有的部分信息，然后进行修改提交*/
		function updateinfo() {
			var phone = /^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/;
			var payphone = $("#payphone").val();
			if($("#name").val() != '') {
				if(phone.test(payphone.trim())) {
					if($("#userCard").val() != '') {
						if($("#userAddr").val() != '') {
							mui.ajax(puburl.url+'/dangxia/user/updateinfo', {
								data: {
									username:$("#username").val(),
									name:$("#name").val(),
									userSex:$('input[name="userSex"]:checked').val(),
									payphone:$("#payphone").val(),
									userCard:$("#userCard").val(),
									userAddr:$("#userAddr").val()
								},
								dataType: 'json', //服务器返回json格式数据
								type: 'post', //HTTP请求类型
								timeout: 10000, //超时时间设置为10秒；
								success: function(data) {
									if(data.code == 0) {
										alert("修改成功");
							
											//获取当前页面
										    var cPage = plus.webview.currentWebview();
										    //获取上一级页面（B页面）
										    var bPage = cPage.opener();
											
										    //关闭当前页面（C页面）、B页面和A页面
										    cPage.close();
			
									}else{
										mui.toast(data.msg);
									}
			
								},
								error: function(xhr, type, errorThrown) { //异常处理；
									console.log(type);
								}
							});
						} else {
							mui.alert("地址不能为空！", "提示", "ok", function() {}, 'div');
							return;
						}
					} else {
						mui.alert("身份证号不能为空！", "提示", "ok", function() {}, 'div');
						return;
					}
				} else {
					mui.alert("请正确填写手机号！", "提示", "ok", function() {}, 'div');
					return;
				}
			} else {
				mui.alert("姓名不能为空！", "提示", "ok", function() {}, 'div');
				return;
			}
		}
	</script>
</body>
</html>